package com.gxa.modules.service.impl;

import com.gxa.common.vo.JsonResult;
import com.gxa.common.vo.PageRecv;
import com.gxa.common.vo.R;
import com.gxa.common.vo.ResultVo;
import com.gxa.modules.mapper.BorrowCenterMapper;
import com.gxa.modules.pojo.*;
import com.gxa.modules.service.BorrowCenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class BorrowCenterServiceImpl implements BorrowCenterService {
    @Autowired(required = false)
    private BorrowCenterMapper borrowCenterMapper;
    @Override
    public JsonResult selectBeforeCredit(PageRecv pageRecv) {
        int param1=(pageRecv.getCurrentPage()-1)*pageRecv.getPageSize();
        pageRecv.setCurrentPage(param1);
        List<Loan> loanList=borrowCenterMapper.selectBeforeCredit(pageRecv);
//        遍历loanSearchList集合获取对应的审核记录表id查询对应的审核时间和员工姓名
        for(Loan loan:loanList){
            List<Audit> auditList=new ArrayList<Audit>();
            if(loan.getApproveLoan().getFirst_id()!=0){
                Audit firstAudit=borrowCenterMapper.selectAuditById(loan.getApproveLoan().getFirst_id());
                auditList.add(firstAudit);
            }
            if(loan.getApproveLoan().getSecond_id()!=0){
                Audit secondAudit=borrowCenterMapper.selectAuditById(loan.getApproveLoan().getSecond_id());
                auditList.add(secondAudit);
            }
            if(loan.getApproveLoan().getThird_id()!=0){
                Audit thirdAudit=borrowCenterMapper.selectAuditById(loan.getApproveLoan().getThird_id());
                auditList.add(thirdAudit);
            }
            loan.setAuditList(auditList);
        }
        System.out.println("loanList"+loanList);
        Integer count=borrowCenterMapper.selectBeforeCreditCount(pageRecv);
        JsonResult jsonResult=new JsonResult();
        jsonResult.setCode(200);
        jsonResult.setMsg("查询成功");
        jsonResult.setData(loanList);
        jsonResult.setCount(count);
        return jsonResult;
    }

    @Override
    public JsonResult selectAllAudit(long loan_id) {
        JsonResult jsonResult=new JsonResult();
        List<Audit> auditList=borrowCenterMapper.selectAllAudit(loan_id);
        jsonResult.setCode(200);
        jsonResult.setData(auditList);
        jsonResult.setMsg("查询成功");
        return jsonResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultVo addAudit(AuditAdd auditAdd) {
        ResultVo resultVo=new ResultVo();
//        先修改loan表
            System.out.println("进来修改Loan表数据");
            borrowCenterMapper.updateLoan(auditAdd);
//        再添加audot表数据
        borrowCenterMapper.addAudit(auditAdd);
        ApproveLoan approveLoan=borrowCenterMapper.selectApproveLoanById(auditAdd.getLoan_id());
//        再添加appprove_loan
        if(approveLoan==null) {
            borrowCenterMapper.addApproveLoan(auditAdd.getLoan_id());
        }
//        再修改approve_loan
        borrowCenterMapper.updateApproveLoan(auditAdd.getLoan_id(),auditAdd.getId(),auditAdd.getState());
       resultVo.setMsg("成功");
       resultVo.setCode(200);
        return resultVo;
    }
}
